<?php
/**
 * 意见反馈
 */
namespace Admin\Controller\Admin;
use Think\Controller;

class FeedbackController extends Controller 
{
	/**
	 *	管理后台菜单初始化与用户登录身份验证
	 */
	public function _initialize() 
	{
		// 判断用户是否登陆
		if (1!=1) //	!session('?regist_id')
		{
			$url = 'http://' . $_SERVER['HTTP_HOST'] .'/tp_bypal/index.php';
			$this->redirect($url);
			// echo "<script>alert('权限不足，无法操作');window.location.href='".$url."';</script>";
			exit;
		}
	}


	/**
	 * 列表页
	 */
	public function feedback_list() 
	{
		$model = D("bypal_cust_feedback");
		$array_data = $model->order('creatime desc')->select();
		foreach ($array_data as $key => $value) 
		{
			$array_data[$key]['creatime'] = date('Y-m-d H:i:s', $value['creatime']);
		}

		$json_data = json_encode($array_data);
		$this->assign('json_data',$json_data);
		// //	搜索查询条件
		// if ($loginSelect != '') 
		// {
		// 	$condition->cust_loadname = array('like', '%'.$loginSelect.'%');
		// }
		// if ($nameSelect != '') 
		// {
		// 	$condition->cust_name = array('like', '%'.$nameSelect.'%');
		// }
		// if ($phoneSelect != '') 
		// {
		// 	$condition->cust_phone = array('like', '%'.$phoneSelect.'%');
		// }
		// if ($emailSelect != '') 
		// {
		// 	$condition->cust_email = array('like', '%'.$emailSelect.'%');
		// }
		// if ($timeSelect != '') 
		// {
		// 	$time = strtotime($timeSelect);
		// 	$condition->cust_recentdate = array(array('egt',$time),array('elt',$time+86400));
		// }
		// $customer_array = $customer->where($condition)->select();
		// $balance_borrow = D("BalanceBorrow");
		// $balance_invest = D("BalanceInvest");
		// foreach ($customer_array as $key => $value) 
		// {
		// 	$customer_array[$key]['cust_recentdate'] = date('Y-m-d H:i:s', $value['cust_recentdate']);
		// 	$balance_borrow_data = $balance_borrow->where('cust_id='.$value['cust_id'])->find();
		// 	$balance_invest_data = $balance_invest->where('cust_id='.$value['cust_id'])->find();
		// 	$customer_array[$key]['fund_balance'] = $balance_borrow_data['fund_balance'] + $balance_invest_data['fund_balance'];
		// }

		// $json = json_encode($customer_array);
		// $this->assign('customer_json',$json);

		$this->display();
	}


	/**
	 * 客户新增
	 */
	public function customer_add() 
	{
		//	检测用户名是否存在
		if ($_POST && array_key_exists('method', $_POST) && $_POST['method'] == 'checkLoadName') 
		{
			$loadName = $_POST['loadName'];

			$model = D("bypal_cust_info");
			$result = $model->where('cust_loadname='.$loadName)->find();
			if ($result) 
			{
				$this->ajaxReturn('exist');
			}
			else
			{
				$this->ajaxReturn('ok');
			}
		}

		if ($_POST && array_key_exists('cust_phone', $_POST) && $_POST['cust_phone']) 
		{
			$model = D('bypal_cust_info');
			$data['cust_name'] = $_POST['cust_name'];			//	姓名
			if ($_POST['cust_sex']) 
			{
				$data['cust_sex'] = $_POST['cust_sex'];			//	性别
			}
			$data['cust_identityid'] = '0';						//	证件类型ID
			$data['cust_identity'] = $_POST['cust_identity'];	//	身份证号码
			$data['cust_phone'] = $_POST['cust_phone'];			//	手机号码
			$data['cust_email'] = $_POST['cust_email'];			//	邮箱
			$data['cust_loadname'] = $_POST['cust_phone'];		//	登录名
			$data['cust_pwd'] = md5('123456');					//	密码
			$data['cust_createdate'] = time();					//	创建时间
			$data['cust_recentdate'] = time();					//	最近登录时间
			if ($result = $model->add($data)) 
			{
				//	借款余额表
				$insert_balance_borrow = D('bypal_balance_borrow');
				$balance_borrow_array['fund_balance'] = '0';
				$balance_borrow_array['last_time'] = date('Y-m-d H:i:s', time());
				$balance_borrow_array['cust_id'] = $result;
				$insert_balance_borrow->add($balance_borrow_array);

				//	投资余额表
				$insert_balance_invest = D('bypal_balance_invest');
				$balance_invest_array['fund_balance'] = '0';
				$balance_invest_array['last_time'] = date('Y-m-d H:i:s', time());
				$balance_invest_array['cust_id'] = $result;
				$insert_balance_invest->add($balance_invest_array);

				//	详细信息表
				$insert_detailinformation = D('bypal_cust_detailinformation');
				$detailinformation_array['cust_id'] = $result;
				$detailinformation_array['status'] = '0';
				$insert_detailinformation->add($detailinformation_array);

				//	联系人信息表
				$insert_linkman = D('bypal_cust_linkman');
				$linkman_array['cust_id'] = $result;
				$linkman_array['status'] = '0';
				$insert_linkman->add($linkman_array);

				//	工作信息表
				$insert_workdetail = D('bypal_cust_workdetail');
				$workdetail_array['cust_id'] = $result;
				$workdetail_array['status'] = '0';
				$insert_workdetail->add($workdetail_array);

				echo "<script>alert('新增客户成功！');window.location.href='../../customer/customer/customer_update?cust_id=".$result."';</script>";
			}
		}

		$this->display();
	}


	/**
	 * 客户编辑
	 */
	public function customer_update() 
	{
		if (IS_GET && I('get.cust_id')) 
		{
			//	基本信息数据
			$model = D("bypal_cust_info");
			$data_array = $model->where('cust_id='.I('get.cust_id'))->find();
			$this->assign('data_array',$data_array);

			//	详细信息数据
			$model_detail = D("bypal_cust_detailinformation");
			$data_detail = $model_detail->where('cust_id='.I('get.cust_id'))->find();
			$this->assign('data_detail',$data_detail);

			//	联系人信息数据
			$model_links = D("bypal_cust_linkman");
			$data_links = $model_links->where('cust_id='.I('get.cust_id'))->find();
			$this->assign('data_links',$data_links);

			//	工作信息数据
			$model_workdetail = D("bypal_cust_workdetail");
			$data_workdetail = $model_workdetail->where('cust_id='.I('get.cust_id'))->find();
			$this->assign('data_workdetail',$data_workdetail);

			//	婚恋状况下拉框数据
			$model_marital = D("bypal_cust_marital");
			$data_marital = $model_marital->select();
			$this->assign('data_marital',$data_marital);

			//	最高学历下拉框数据
			$model_education = D("bypal_cust_education");
			$data_education = $model_education->select();
			$this->assign('data_education',$data_education);

			//	最高学位下拉框数据
			$model_degree = D("bypal_cust_degree");
			$data_degree = $model_degree->select();
			$this->assign('data_degree',$data_degree);

			//	居住状况下拉框数据
			$model_livestatus = D("bypal_cust_livestatus");
			$data_livestatus = $model_livestatus->select();
			$this->assign('data_livestatus',$data_livestatus);

			//	联系人关系下拉框数据
			$model_relation = D("bypal_cust_relation");
			$data_relation = $model_relation->select();
			$this->assign('data_relation',$data_relation);

			//	工作身份类型下拉框数据
			$model_worktype = D("bypal_cust_typename");
			$data_worktype = $model_worktype->select();
			$this->assign('data_worktype',$data_worktype);

			//	行业下拉框数据
			$model_profession = D("bypal_profession_info");
			$data_profession = $model_profession->select();
			$this->assign('data_profession',$data_profession);

			//	职业下拉框数据
			$model_position = D("bypal_cust_position");
			$data_position = $model_position->select();
			$this->assign('data_position',$data_position);

			//	职务下拉框数据
			$model_duty = D("bypal_cust_duty");
			$data_duty = $model_duty->select();
			$this->assign('data_duty',$data_duty);

			//	职称下拉框数据
			$model_title = D("bypal_cust_title");
			$data_title = $model_title->select();
			$this->assign('data_title',$data_title);

			$this->display();
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}
	}


	/**
	 * 客户编辑#####基本信息修改
	 */
	public function customer_update_base() 
	{
		if ($_POST && array_key_exists('base', $_POST) && $_POST['base']) 
		{
			$model = D("bypal_cust_info");
			$model->cust_name = $_POST['cust_name'];
			if ($_POST['cust_sex']) 
			{
				$model->cust_sex = $_POST['cust_sex'];
			}
			$model->cust_phone = $_POST['cust_phone'];
			$model->cust_email = $_POST['cust_email'];
			$model->cust_identity = $_POST['cust_identity'];
			$model->where('cust_id='.$_POST['base'])->save();

			echo "<script>window.location.href='../../customer/customer/customer_update?cust_id=".$_POST['base']."';</script>";
			exit;
		}
	}


	/**
	 * 客户编辑#####详细信息修改
	 */
	public function customer_update_detail() 
	{
		if ($_POST && array_key_exists('detail', $_POST) && $_POST['detail']) 
		{
			$model = D("bypal_cust_detailinformation");
			if ($model->where('cust_id='.$_POST['detail'])->find()) 
			{
				$update = D("bypal_cust_detailinformation");
				$update->marry_status_id = $_POST['marry_status_id'];
				$update->communication_province = $_POST['communication_province'];
				if ($_POST['communication_city']) 
				{
					$update->communication_city = $_POST['communication_city'];
				}
				if ($_POST['communication_district']) 
				{
					$update->communication_district = $_POST['communication_district'];
				}
				$update->communication_address = $_POST['communication_address'];
				$update->communication_postcode = $_POST['communication_postcode'];
				$update->education_status_id = $_POST['education_status_id'];
				$update->degree_status_id = $_POST['degree_status_id'];
				$update->live_province = $_POST['live_province'];
				if ($_POST['live_city']) 
				{
					$update->live_city = $_POST['live_city'];
				}
				if ($_POST['live_district']) 
				{
					$update->live_district = $_POST['live_district'];
				}
				$update->live_address = $_POST['live_address'];
				$update->live_postcode = $_POST['live_postcode'];
				$update->live_status = $_POST['live_status'];
				if ($_POST['marry_status_id']!='' && $_POST['communication_province']!='' && $_POST['communication_city'] && $_POST['communication_district'] && $_POST['communication_address']!='' && $_POST['communication_postcode']!='' && $_POST['education_status_id']!='' && $_POST['degree_status_id']!='' && $_POST['live_province']!='' && $_POST['live_city'] && $_POST['live_district'] && $_POST['live_address']!='' && $_POST['live_postcode']!='' && $_POST['live_status']!='') 
				{
					$update->status = '1';
				}
				else
				{
					$update->status = '0';
				}
				$update->where('cust_id='.$_POST['detail'])->save();
			}

			echo "<script>window.location.href='../../customer/customer/customer_update?cust_id=".$_POST['detail']."';</script>";
			exit;
		}
	}


	/**
	 * 客户编辑#####联系人信息修改
	 */
	public function customer_update_links() 
	{
		if ($_POST && array_key_exists('links', $_POST) && $_POST['links']) 
		{
			$model = D("bypal_cust_linkman");
			if ($model->where('cust_id='.$_POST['links'])->find()) 
			{
				$update = D("bypal_cust_linkman");
				$update->first_linkman_name = $_POST['first_linkman_name'];
				$update->first_linkman_type = $_POST['first_linkman_type'];
				$update->first_linkman_phone = $_POST['first_linkman_phone'];
				$update->first_linkman_province = $_POST['first_linkman_province'];
				if ($_POST['first_linkman_city']) 
				{
					$update->first_linkman_city = $_POST['first_linkman_city'];
				}
				if ($_POST['first_linkman_district']) 
				{
					$update->first_linkman_district = $_POST['first_linkman_district'];
				}
				$update->first_linkman_address = $_POST['first_linkman_address'];
				$update->second_linkman_name = $_POST['second_linkman_name'];
				$update->second_linkman_type = $_POST['second_linkman_type'];
				$update->second_linkman_phone = $_POST['second_linkman_phone'];
				if ($_POST['first_linkman_name']!='' && $_POST['first_linkman_type']!='' && $_POST['first_linkman_phone']!='' && $_POST['first_linkman_province']!='' && $_POST['first_linkman_city'] && $_POST['first_linkman_district'] && $_POST['first_linkman_address']!='' && $_POST['second_linkman_name']!='' && $_POST['second_linkman_type']!='' && $_POST['second_linkman_phone']!='') 
				{
					$update->status = '1';
				}
				else
				{
					$update->status = '0';
				}
				$update->where('cust_id='.$_POST['links'])->save();
			}

			echo "<script>window.location.href='../../customer/customer/customer_update?cust_id=".$_POST['links']."';</script>";
			exit;
		}
	}


	/**
	 * 客户编辑#####工作信息修改
	 */
	public function customer_update_workinfo() 
	{
		if ($_POST && array_key_exists('workinfo', $_POST) && $_POST['workinfo']) 
		{
			$model = D("bypal_cust_workdetail");
			if ($model->where('cust_id='.$_POST['workinfo'])->find()) 
			{
				$update = D("bypal_cust_workdetail");
				$update->current_status = $_POST['current_status'];
				$update->company_industry = $_POST['company_industry'];
				$update->profession_status_id = $_POST['profession_status_id'];
				$update->duty_status = $_POST['duty_status'];
				$update->title_status = $_POST['title_status'];
				$update->company_name = $_POST['company_name'];
				$update->company_province = $_POST['company_province'];
				if ($_POST['company_city']) 
				{
					$update->company_city = $_POST['company_city'];
				}
				if ($_POST['company_district']) 
				{
					$update->company_district = $_POST['company_district'];
				}
				$update->company_address = $_POST['company_address'];
				$update->company_email = $_POST['company_email'];
				if ($_POST['current_status']!='' && $_POST['company_industry']!='' && $_POST['profession_status_id']!='' && $_POST['duty_status']!='' && $_POST['title_status']!='' && $_POST['company_name']!='' && $_POST['company_province']!='' && $_POST['company_city'] && $_POST['company_district'] && $_POST['company_address']!='' && $_POST['company_email']!='') 
				{
					$update->status = '1';
				}
				else
				{
					$update->status = '0';
				}
				$update->where('cust_id='.$_POST['workinfo'])->save();
			}

			echo "<script>window.location.href='../../customer/customer/customer_update?cust_id=".$_POST['workinfo']."';</script>";
			exit;
		}
	}


	/**
	 * 客户详情
	 */
	public function customer_see() 
	{
		if (IS_GET && I('get.cust_id')) 
		{
			$cust_id = I('get.cust_id');
			$this->assign('cust_id',$cust_id);

			//	1.材料审核信息
			$model_reference = D("bypal_cust_reference");
			$data_reference = $model_reference->where('ref_custid='.$cust_id)->select();
			foreach ($data_reference as $key => $value) 
			{
				//	类型
				$model_refertype = D("bypal_cust_refertype");
				$data_refertype = $model_refertype->where('refertype_id='.$value['ref_type'])->find();
				$data_reference[$key]['refertype_name'] = $data_refertype['refertype_name'];

				//	附件
				$model_referenceFile = D("bypal_cust_referencefile");
				$data_referenceFile = $model_referenceFile->where('ref_id='.$value['ref_id'])->find();
				$data_reference[$key]['referenceFile'] = 'http://' . $_SERVER['HTTP_HOST'] . '/Public/Upload/UploadAudit/' . $data_referenceFile['ref_annex'];

				//	审核状态
				$model_status = D("bypal_cust_status");
				$data_status = $model_status->where('status_id='.$value['ref_status'])->find();
				$data_reference[$key]['status_name'] = $data_status['status_name'];

				//	时间
				$data_reference[$key]['ref_createdate'] = date('Y-m-d', $value['ref_createdate']);
			}
			$json = json_encode($data_reference);
			$this->assign('json',$json);

			//	2.借款信息
			$model_borrow = D("bypal_borrow_info");
			$data_borrow = $model_borrow->where('borrow_custid='.$cust_id)->select();
			foreach ($data_borrow as $key => $value) 
			{
				//	还款方式
				$model_repay = D("bypal_repay_method");
				$data_repay = $model_repay->where('repay_id='.$value['borrow_repayid'])->find();
				$data_borrow[$key]['repay'] = $data_repay['repay_name'];

				//	借款状态
				$model_borrowStatus = D("bypal_borrow_status");
				$data_borrowStatus = $model_borrowStatus->where('bsta_id='.$value['borrow_stateid'])->find();
				$data_borrow[$key]['status'] = $data_borrowStatus['bsta_name'];
			}
			$json_borrow = json_encode($data_borrow);
			$this->assign('json_borrow',$json_borrow);

			//	3.投资信息
			$model_invest = D("bypal_invest_detail");
			$data_invest = $model_invest->where('customer_id='.$cust_id)->select();
			foreach ($data_invest as $key => $value) 
			{
				$model_investProject = D("bypal_invest_project");
				$data_investProject = $model_investProject->where('invest_id='.$value['invest_id'])->find();
				$data_invest[$key]['invest_time_limit'] = $data_investProject['invest_time_limit'];

				$model_investType = D("bypal_invest_type");
				$data_investType = $model_investType->where('invest_type_id='.$data_investProject['invest_type_id'])->find();
				$data_invest[$key]['invest_type_name'] = $data_investType['invest_type_name'];

				$model_investStatus = D("bypal_invest_status");
				$data_investStatus = $model_investStatus->where('invest_status_id='.$data_investProject['invest_status_id'])->find();
				$data_invest[$key]['invest_status_name'] = $data_investStatus['invest_status_name'];
			}
			$json_invest = json_encode($data_invest);
			$this->assign('json_invest',$json_invest);

			//	4.信息验证信息
			$model_info = D("bypal_impl_validate");
			$data_info = $model_info->where('cust_id='.$cust_id)->select();
			foreach ($data_info as $key => $value) 
			{
				//	验证结果
				if ($value['result'] == '1') 
				{
					$data_info[$key]['result'] = '成功';
				}
				else
				{
					$data_info[$key]['result'] = '失败';
				}

				//	验证类型
				if ($value['type'] == '1') 
				{
					$data_info[$key]['type'] = '身份证认证';
				}
				else
				{
					$data_info[$key]['type'] = '银行卡认证';
				}
			}
			$json_info = json_encode($data_info);
			$this->assign('json_info',$json_info);

			$this->display();
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}
	}


	/**
	 * 客户详情#####获取信息数据
	 */
	public function get_customer_see() 
	{
		if (IS_GET && I('get.cust_id')) 
		{
			//	基本信息数据
			$model = D("bypal_cust_info");
			$data_array = $model->where('cust_id='.I('get.cust_id'))->find();
			$this->assign('data_array',$data_array);

			//	详细信息数据
			$model_detail = D("bypal_cust_detailinformation");
			$data_detail = $model_detail->where('cust_id='.I('get.cust_id'))->find();
			$this->assign('data_detail',$data_detail);

			//	联系人信息数据
			$model_links = D("bypal_cust_linkman");
			$data_links = $model_links->where('cust_id='.I('get.cust_id'))->find();
			$this->assign('data_links',$data_links);

			//	工作信息数据
			$model_workdetail = D("bypal_cust_workdetail");
			$data_workdetail = $model_workdetail->where('cust_id='.I('get.cust_id'))->find();
			$this->assign('data_workdetail',$data_workdetail);

			//	婚恋状况下拉框数据
			$model_marital = D("bypal_cust_marital");
			$data_marital = $model_marital->where('m_id='.$data_detail['marry_status_id'])->find();
			$this->assign('data_marital',$data_marital);

			//	最高学历下拉框数据
			$model_education = D("bypal_cust_education");
			$data_education = $model_education->where('edu_id='.$data_detail['education_status_id'])->find();
			$this->assign('data_education',$data_education);

			//	最高学位下拉框数据
			$model_degree = D("bypal_cust_degree");
			$data_degree = $model_degree->where('deg_id='.$data_detail['degree_status_id'])->find();
			$this->assign('data_degree',$data_degree);

			//	居住状况下拉框数据
			$model_livestatus = D("bypal_cust_livestatus");
			$data_livestatus = $model_livestatus->where('li_id='.$data_detail['live_status'])->find();
			$this->assign('data_livestatus',$data_livestatus);

			//	联系人关系下拉框数据
			$model_relation = D("bypal_cust_relation");
			$data_relation = $model_relation->where('r_id='.$data_links['first_linkman_type'])->find();
			$this->assign('data_relation',$data_relation);

			$data_relation2 = $model_relation->where('r_id='.$data_links['second_linkman_type'])->find();
			$this->assign('data_relation2',$data_relation2);

			//	工作身份类型下拉框数据
			$model_worktype = D("bypal_cust_typename");
			$data_worktype = $model_worktype->where('cust_typeid='.$data_workdetail['current_status'])->find();
			$this->assign('data_worktype',$data_worktype);

			//	行业下拉框数据
			$model_profession = D("bypal_profession_info");
			$data_profession = $model_profession->where('id='.$data_workdetail['company_industry'])->find();
			$this->assign('data_profession',$data_profession);

			//	职业下拉框数据
			$model_position = D("bypal_cust_position");
			$data_position = $model_position->where('pos_id='.$data_workdetail['profession_status_id'])->find();
			$this->assign('data_position',$data_position);

			//	职务下拉框数据
			$model_duty = D("bypal_cust_duty");
			$data_duty = $model_duty->where('du_id='.$data_workdetail['duty_status'])->find();
			$this->assign('data_duty',$data_duty);

			//	职称下拉框数据
			$model_title = D("bypal_cust_title");
			$data_title = $model_title->where('ti_id='.$data_workdetail['title_status'])->find();
			$this->assign('data_title',$data_title);
			
			$this->display();
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}
	}


	/**
	 * 客户详情#####材料审核信息@附件审核
	 */
	public function reference_check() 
	{
		if (IS_GET && I('get.ref_id')) 
		{
			$model_reference = D("bypal_cust_reference");
			$data_reference = $model_reference->where('ref_id='.I('get.ref_id'))->find();
			$this->assign('data_reference',$data_reference);

			$model_custInfo = D("bypal_cust_info");
			$data_custInfo = $model_custInfo->where('cust_id='.$data_reference['ref_custid'])->find();
			$this->assign('data_custInfo',$data_custInfo);

			$model_refertype = D("bypal_cust_refertype");
			$data_refertype = $model_refertype->where('refertype_id='.$data_reference['ref_type'])->find();
			$this->assign('data_refertype',$data_refertype);

			$model_file = D("bypal_cust_referencefile");
			$array_file = $model_file->where('ref_id='.$data_reference['ref_id'])->select();
			$this->assign('array_file',$array_file);

			$this->display();
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}
	}


	/**
	 * 客户详情#####材料审核信息@附件审核提交
	 */
	public function reference_check_Submit() 
	{
		if ($_POST && array_key_exists('ref_id', $_POST) && $_POST['ref_id']) 
		{
			$update = D("bypal_cust_reference");
			$update->ref_point = $_POST['point'];
			$update->ref_status = $_POST['checkstatus'];
			$update->ref_conftime = time();
			if ($_POST['period']) 
			{
				$update->ref_explain = $_POST['period'];
			}
			else
			{
				$update->ref_explain = '0';
			}

			if ($update->where('ref_id='.$_POST['ref_id'])->save()) 
			{
				echo "<script>alert('恭喜您！修改成功');window.location.href='../../customer/customer/customer_see?cust_id=".$_POST['custId']."';</script>";
				exit;
			}
			else
			{
				echo "<script>alert('很遗憾！保存失败');window.location.href='../../customer/customer/customer_see?cust_id=".$_POST['custId']."';</script>";
				exit;
			}
		}
	}

}